
#include "GroupedOpera.h"

GroupedOpera::GroupedOpera(BGroup* master, BScene* scene)
{
	_scene = scene;
	_master = master;
}

void GroupedOpera::addHit(BPickHit* hit)
{
	_pickHits.append(hit);
}

bool GroupedOpera::redo()
{
	if (_master)
	{
		for (int i = 1; i < _pickHits.size(); i++)
		{
			BPickHit* hit = _pickHits[i];
			if (BShape* shape = hit->shape())
			{
				BHolder<BShape> holder(shape);
				if (BNode* parent = hit->node())
					parent->removeShape(shape);
				else if (BGroup* group = hit->group())
					group->remove(shape);
				else if (_scene)
					_scene->remove(shape);
				_master->insert(shape);
				continue;
			}
			if (BNode* node = hit->node())
			{
				BHolder<BNode> holder(node);
				if (BGroup* group = hit->group())
					group->remove(node);
				else if (_scene)
					_scene->remove(node);
				_master->insert(node);
				continue;
			}
			if (BGroup* other = hit->group())
			{
				for (int i = 0; i < other->shapeCount(); i++)
					_master->insert(other->shape(i));
				other->clearShapes();
				for (int i = 0; i < other->nodeCount(); i++)
					_master->insert(other->node(i));
				other->clearNodes();
				for (int i = 0; i < other->restraintCount(); i++)
					_master->insert(other->restraint(i));
				other->clearRestraints();
				for (int i = 0; i < other->childCount(); i++)
					_master->insert(other->child(i));
				other->clearChildren();
				if (_scene)
					_scene->remove(other);
			}
		}
		return true;
	}
	return false;
}
bool GroupedOpera::undo()
{
	return false;
}

